package ru.zombator.taskman.server.query.corba.manual;

import org.omg.CORBA.Any;
import org.omg.CORBA.ORB;
import org.omg.PortableInterceptor.ForwardRequest;
import org.omg.PortableInterceptor.InvalidSlot;
import org.omg.PortableInterceptor.ServerRequestInfo;
import org.omg.PortableInterceptor.ServerRequestInterceptor;

import com.sun.corba.se.impl.interceptors.ServerRequestInfoImpl;
import com.sun.corba.se.spi.legacy.connection.Connection;

public class IPAddrInterceptor extends org.omg.CORBA.LocalObject implements
		ServerRequestInterceptor {
	private static final long serialVersionUID = 1L;
	private int slotId;
	
	public IPAddrInterceptor(int slotId) {
		this.slotId = slotId;
	}

	public void receive_request_service_contexts(ServerRequestInfo ri)
			throws ForwardRequest {
		Connection connection = ((ServerRequestInfoImpl) ri).connection();
		java.net.Socket clientSocket = connection.getSocket();
		String ip = clientSocket.getInetAddress().getHostAddress();
		
		Any ipaddr = ORB.init().create_any();
		ipaddr.insert_string(ip);
		try {
			ri.set_slot(slotId, ipaddr);
		} catch (InvalidSlot e) {
			e.printStackTrace();
		}
	}

	@Override
	public void receive_request(ServerRequestInfo ri) throws ForwardRequest {
		
	}

	@Override
	public void send_exception(ServerRequestInfo ri) throws ForwardRequest {
		
	}

	@Override
	public void send_other(ServerRequestInfo ri) throws ForwardRequest {
		
	}

	@Override
	public void send_reply(ServerRequestInfo ri) {
		
	}

	@Override
	public void destroy() {
		
	}

	@Override
	public String name() {
		return "ipaddrinterceptor";
	}
}